import { AudioManager1 } from "../../manager/AudioManager1";
import { EventManager } from "../../manager/EventManager";
import TouchScale from "../../item/Common/TouchScale";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../../base/EnumIndex";
import { Tools } from "../../common/Tools";
import Level_23_Base from "./Level_23_Base";
import LevelBase from "../../LevelBase";
import LvItem from "../../LvItem";

const { ccclass, property } = cc._decorator;
// 六个问题 90秒
// 八个到十个问题 120秒
@ccclass
export default class LevelPre_23 extends LevelBase {
    @property(cc.Node)
    maskNode: cc.Node = null;
    @property(cc.Node)
    content: cc.Node = null;
    @property(cc.Node)
    tipLabNode: cc.Node = null;
    @property([cc.Label])
    lbQ: cc.Label[] = [];

    /** 本关的交互配置 */
    interJson: any = null;

    /**问题进度 */
    progress: number = 0;

    /**计分板1 */
    score1: number = 0;
    /**计分板2 */
    score2: number = 0;

    huaNode: cc.Node = null;   //画的节点

    // onLoad () {}

    start() {
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
    }
    onDestroy(): void {
        super.onDestroy();
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
        this.unscheduleAllCallbacks();
    }
    init(huaPre: cc.Prefab) {
        this.huaNode = cc.instantiate(huaPre);
        this.maskNode.addChild(this.huaNode);

        // 适配
        let widthScale = this.maskNode.width / this.huaNode.width;
        let heightScale = this.maskNode.height / this.huaNode.height;
        this.huaNode.scale = Math.min(widthScale, heightScale);

        if (this.huaNode.getComponent(TouchScale)) {
            this.huaNode.getComponent(TouchScale).maskNode = this.maskNode;
        }
    }


    clickA1() {
        EventManager.ins.emit(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, "finish_ans_" + this.progress + "_1");
    }

    clickA2() {
        EventManager.ins.emit(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, "finish_ans_" + this.progress + "_2");
    }

    clickA3() {
        EventManager.ins.emit(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, "finish_ans_" + this.progress + "_3");
    }

    finishTween(finishStr: any, lvItem: LvItem) {
        if (typeof finishStr != "string") {
            if (finishStr.sayInfo) {   //人物说话
                let isShow = false;
                if (finishStr.isShow) {
                    isShow = true;
                }
                this.sayNodeTween(finishStr.sayInfo, isShow);
            }
            if (finishStr.str) {  //提示说话
                this.showTipInfo(finishStr);
            }
            return;
        }

        //显示前全部选项
        if (finishStr.includes("setQuestion")) {
            this.progress++;
            let progress = this.progress.toString();
            let questions = this.interJson["questions"][progress];
            if (questions) {
                for (let i = 0; i < questions.length; i++) {
                    this.lbQ[i].string = questions[i];
                }
            }

            this.content.active = true;
            this.playQuestionAnim(true);
            return;
        }

        if (finishStr.includes("ans")) {

            this.playQuestionAnim(false);
            let _progress = Number(finishStr.split("_")[2]);
            let answer = Number(finishStr.split("_")[3]);
            if (_progress == this.progress) {
                let tween = this.getInterTwForName("ans" + this.progress + "_" + answer)
                if (!tween) return;
                this.runNodeTween(this.huaNode, tween.interTween);
            }
            return;
        }
        if (finishStr.includes("hidecontent")) {
            this.playQuestionAnim(false);
            return;
        }
        if (finishStr.includes("pauseTime")) {
            EventManager.ins.emit(EVENT_NAME_ENUM.PAUSE_TIME);
        }
        if (finishStr.includes("tip_")) {  //刷新是否胜利
            let str = finishStr.split("tip_")[1];
            this.showTipLab(str);
            return;
        }
    }
    lastAudio: string = null;
    /** 执行提示文字事件 */
    async showTipInfo(sayInfo: any) {
        let tempT = 3;
        if (sayInfo.audio) {
            if (this.lastAudio) {
                AudioManager1.stopAudioForName(this.lastAudio);
            }
            let audio = await AudioManager1.playAudio(sayInfo.audio.resUrl, sayInfo.audio.value, BUNDLE_TYPE_ENUM.LEVEL);
            tempT = cc.audioEngine.getDuration(audio) + 0.2;
            this.lastAudio = sayInfo.audio.resUrl;
        } else if (sayInfo.time) {
            tempT = sayInfo.time;
        }
        if (sayInfo.str) {

            this.showTipNodeLab(sayInfo.str, tempT, sayInfo);
        }
        if (sayInfo.armAni) {
            this.runArmAni(sayInfo.armAni, sayInfo.armAni.shuo, sayInfo.armAni.node);
        }
        return tempT;
    };
    /** 显示提示文字  time 显示时间 */
    showTipNodeLab(str: string, time: number, sayInfo: any) {
        cc.Tween.stopAllByTarget(this.tipLabNode);
        this.tipLabNode.active = AudioManager1.isSubtitle;

        let lab = this.tipLabNode.getComponentInChildren(cc.Label);
        if (str != "") {
            lab.string = str;
        }
        if (sayInfo.audio && sayInfo.audio.show) {
            cc.tween(this.tipLabNode)
                .to(0.5, { opacity: 255 })
                .delay(time)
                .call(() => {
                    if (sayInfo.armAni) {
                        this.runArmAni(sayInfo.armAni, sayInfo.armAni.daiji, sayInfo.armAni.node);
                    }
                }).start();
        } else {
            cc.tween(this.tipLabNode)
                .to(0.5, { opacity: 255 })
                .delay(time)
                .call(() => {
                    if (sayInfo.armAni) {
                        this.runArmAni(sayInfo.armAni, sayInfo.armAni.daiji, sayInfo.armAni.node);
                    }
                }).to(0.5, { opacity: 0 }).start();
        }

    };

    /** 显示提示文字 */
    showTipLab(str: string) {
        let tipNode = cc.instantiate(this.tipLabNode);
        tipNode.active = true;
        tipNode.parent = this.tipLabNode.parent;
        tipNode.getChildByName("tipLab").getComponent(cc.Label).string = str;


        this.scheduleOnce(() => {
            tipNode.getChildByName("tipBg").width = tipNode.getChildByName("tipLab").width + 100;
        }, 0);

        cc.tween(tipNode)
            .delay(0.5)
            .by(1, { y: 200 })
            .by(0.5, { y: 100, opacity: -255 })
            .call((target: cc.Node) => {
                target.destroy();
            }).start();
    };
    //播放全部显示动画
    playQuestionAnim(isAwake = true) {
        let animation = this.content.getComponent(cc.Animation);
        if (isAwake) {
            animation.play("question");
        } else {
            animation.play("questionOut");
            this.scheduleOnce(() => {
                this.content.active = false;
            }, 0.5)
        }
        let level23Base = cc.find("Canvas").getComponentInChildren(Level_23_Base);
        if (!level23Base) {
            return
        }
        let autoAnswer = level23Base.autoAnswer;
        if (autoAnswer && isAwake) {
            this.scheduleOnce(() => {
                if (!level23Base.isSelect) {
                    let randomAns = Tools.random(1, 3);
                    EventManager.ins.emit(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, "finish_ans_" + this.progress + "_" + randomAns);
                }
            }, 3)
        }
    }

    async sayNodeTween(sayInfo: any, isShow: boolean = false) {
        let sayNode = this.getChildForName(sayInfo.node);
        if (sayInfo.str && AudioManager1.isSubtitle) {
            sayNode.active = true;
            sayNode.getComponent(LvItem).setLabString(sayInfo.str);
        }

        if (sayInfo.armAni) {
            this.runArmAni(sayInfo.armAni, sayInfo.armAni.shuo, sayNode);
        }
        if (sayInfo.armAni2) {
            this.runArmAni(sayInfo.armAni2, sayInfo.armAni2.shuo, sayNode);
        }

        if (sayInfo.audio) {
            // AudioManager.stopAllAudio();
            let audio = await AudioManager1.playAudio(sayInfo.audio.resUrl, sayInfo.audio.value, BUNDLE_TYPE_ENUM.LEVEL);

            cc.Tween.stopAllByTarget(sayNode);
            let tempT = cc.audioEngine.getDuration(audio);
            cc.tween(sayNode)
                .delay(tempT)
                .call(() => {
                    if (!isShow) {
                        sayNode.active = false;
                    }
                    if (sayInfo.armAni) {
                        this.runArmAni(sayInfo.armAni, sayInfo.armAni.daiji, sayNode);
                    }
                    if (sayInfo.armAni2) {
                        this.runArmAni(sayInfo.armAni2, sayInfo.armAni.daiji, sayNode);
                    }
                }).start();
        }
    };

    /** 通过sayInfo执行龙骨动画 */
    runArmAni(armAni: any, aniName: string, sayNode?: cc.Node) {
        if (!armAni) { return; }
        let node = sayNode.parent;
        if (armAni.node) {
            node = this.getNodeForName(armAni.node);
        }
        node.getComponent(LvItem).playArmtureAni(aniName);
    };
}